//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录 
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code;
        var that = this;
        var userinfo;
        wx.getUserInfo({
          success:function(res){
            userinfo = res.userInfo;

            that.core('post','create_xcx_user',{
              'nickname': userinfo.nickName,
              'headpic': userinfo.avatarUrl,
              'js_code': code
            },(res=>{
                that.globalData.openid = res.data.open_id;
            console.log(that.globalData)
            }))
          }

        })

      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {

        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log(res)
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              wx.setStorageSync('userInfo', res.userInfo)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    wx.onMemoryWarning(function () {
      console.log('onMemoryWarningReceive', '内存')
    })

    // 获取系统状态栏信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        this.globalData.Custom = custom;
        this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
      }
    })


    console.log('zheli yuinxu')
    // //获取用户的 openid
    // var that = this
    // var user = wx.getStorageSync('user') || {};
    // var userInfo = wx.getStorageSync('userInfo') || {};
    // if ((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) && (!userInfo.nickName)) {
    //   wx.login({
    //     success: function (res) {
    //       if (res.code) {
    //         wx.getUserInfo({
    //           success: function (res) {
    //             var objz = {};
    //             objz.avatarUrl = res.userInfo.avatarUrl;
    //             objz.nickName = res.userInfo.nickName;
    //             //console.log(objz);
    //             wx.setStorageSync('userInfo', objz);//存储userInfo
    //           }
    //         });
    //         var d = that.globalData;//这里存储了appid、secret、token串  
    //         var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code';
    //         wx.request({
    //           url: l,
    //           data: {},
    //           method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
    //           // header: {}, // 设置请求的 header  
    //           success: function (res) {
    //             var obj = {};
    //             obj.openid = res.data.openid;
    //             obj.expires_in = Date.now() + res.data.expires_in;
    //             //console.log(obj);
    //             wx.setStorageSync('user', obj);//存储openid  
    //             var opid = wx.getStorageSync('user').openid;//存入全局变量
    //           }
    //         });
    //       } else {
    //         console.log('获取用户登录态失败！' + res.errMsg)
    //       }
    //     }
    //   });
    // }


  },


  core: function (methods, url, data, callback, errFun) {
    const host = 'https://xkcloude.hfhouse.com/routineclient/'; //请求域名
    wx.request({

      url: host + url,

      method: methods, // 请求类型 get post

      // header: {

      //   'content-type': methods == 'GET' ? 'application/json' : 'application/x-www-form-urlencoded'

      // },
      headers: {
        'Content-Type': 'application/json'
      },

      dataType: 'json',

      data: data,

      success: function (res) {

        callback(res.data);

      },

      fail: function (err) {

        errFun(err);

      }

    })

  },

  globalData: {
    hostUrl: "https://wxapp.becypress.com/",
    cityCode: "340100",
    userInfo: null,
    api: "https://xkcloude.hfhouse.com/routineclient/",
    appid: 'wx49650e763e3e3dd4',//appid
    secret: '4b07085cf04689159d1b0246eb493646',//小程序秘钥
    openid:''
  },

})